use crate::public::{
  KeybdKey::{self, *},
  MouseButton::{self, *},
};

/// Keycodes may be found here: https://eastmanreference.com/complete-list-of-applescript-key-codes
// TODO: add new keys, alter some names to match existing
impl From<KeybdKey> for u64 {
  fn from(key: KeybdKey) -> u64 {
    match key {
      DeleteKey => 0x33,
      TabKey => 0x30,
      EnterKey => 0x24,
      EscapeKey => 0x35,
      SpaceKey => 0x31,
      LeftKey => 0x7b,
      UpKey => 0x7e,
      RightKey => 0x7c,
      DownKey => 0x7d,
      Numrow0Key => 0x1d,
      Numrow1Key => 0x12,
      Numrow2Key => 0x13,
      Numrow3Key => 0x14,
      Numrow4Key => 0x15,
      Numrow5Key => 0x17,
      Numrow6Key => 0x16,
      Numrow7Key => 0x1a,
      Numrow8Key => 0x1c,
      Numrow9Key => 0x19,
      // EqualKey => 0x18,
      // MinusKey => 0x1B,
      AKey => 0x00,
      BKey => 0x0b,
      CKey => 0x08,
      DKey => 0x02,
      EKey => 0x0e,
      FKey => 0x03,
      GKey => 0x05,
      HKey => 0x04,
      IKey => 0x22,
      JKey => 0x26,
      KKey => 0x28,
      LKey => 0x25,
      MKey => 0x2e,
      NKey => 0x2d,
      OKey => 0x1f,
      PKey => 0x23,
      QKey => 0x0c,
      RKey => 0x0f,
      SKey => 0x01,
      TKey => 0x11,
      UKey => 0x20,
      VKey => 0x09,
      WKey => 0x0d,
      XKey => 0x07,
      YKey => 0x10,
      ZKey => 0x06,
      Numpad0Key => 0x52,
      Numpad1Key => 0x53,
      Numpad2Key => 0x54,
      Numpad3Key => 0x55,
      Numpad4Key => 0x56,
      Numpad5Key => 0x57,
      Numpad6Key => 0x58,
      Numpad7Key => 0x59,
      Numpad8Key => 0x5b,
      Numpad9Key => 0x5c,
      // NumpadDecimalKey => 0x41,
      // NumpadMultiply => 0x43,
      // NumpadPlusKey => 0x45,
      // NumpadClearKey => 0x47,
      // NumpadDivideKey => 0x4B,
      // NumpadEnterKey => 0x4C,
      // NumpadMinusKey => 0x4E,
      // NumpadEqualsKey => 0x51,
      F1Key => 0x7a,
      F2Key => 0x78,
      F3Key => 0x63,
      F4Key => 0x76,
      F5Key => 0x60,
      F6Key => 0x61,
      F7Key => 0x62,
      F8Key => 0x64,
      F9Key => 0x65,
      F10Key => 0x6d,
      F11Key => 0x67,
      F12Key => 0x6f,
      F13Key => 0x69,
      F14Key => 0x6b,
      F15Key => 0x71,
      F16Key => 0x6a,
      F17Key => 0x40,
      F18Key => 0x4f,
      F19Key => 0x50,
      F20Key => 0x5a,
      CommandKey => 0x37,
      LShiftKey => 0x38,
      // CapsLock => 0x39,
      // OptionKey => 0x3A,
      // Control => 0x3B,
      RShiftKey => 0x3c,
      // ROption => 0x3D,
      // RControl => 0x3E,
      // Function => 0x3F,
      HomeKey => 0x73,
      // RBracket => 0x1E,
      // LBracket => 0x21,
      // QuoteKey => 0x27,
      // Semicolon => 0x29,
      // Backslash => 0x2A,
      // CommaKey => 0x2B,
      // SlashKey => 0x2C,
      // PeriodKey => 0x2F,
      OtherKey(code) => code,
      _ => panic!(""),
    }
  }
}

impl From<u64> for KeybdKey {
  fn from(code: u64) -> KeybdKey {
    match code {
      0x33 => DeleteKey,
      0x30 => TabKey,
      0x24 => EnterKey, // RETURN key
      0x35 => EscapeKey,
      0x31 => SpaceKey,
      0x7b => LeftKey,
      0x7e => UpKey,
      0x7c => RightKey,
      0x7d => DownKey,
      0x1d => Numrow0Key,
      0x12 => Numrow1Key,
      0x13 => Numrow2Key,
      0x14 => Numrow3Key,
      0x15 => Numrow4Key,
      0x17 => Numrow5Key,
      0x16 => Numrow6Key,
      0x1a => Numrow7Key,
      0x1c => Numrow8Key,
      0x19 => Numrow9Key,
      // 0x18 => EqualKey,
      // 0x1B => MinusKey,
      0x00 => AKey,
      0x0b => BKey,
      0x08 => CKey,
      0x02 => DKey,
      0x0e => EKey,
      0x03 => FKey,
      0x05 => GKey,
      0x04 => HKey,
      0x22 => IKey,
      0x26 => JKey,
      0x28 => KKey,
      0x25 => LKey,
      0x2e => MKey,
      0x2d => NKey,
      0x1f => OKey,
      0x23 => PKey,
      0x0c => QKey,
      0x0f => RKey,
      0x01 => SKey,
      0x11 => TKey,
      0x20 => UKey,
      0x09 => VKey,
      0x0d => WKey,
      0x07 => XKey,
      0x10 => YKey,
      0x06 => ZKey,
      0x52 => Numpad0Key,
      0x53 => Numpad1Key,
      0x54 => Numpad2Key,
      0x55 => Numpad3Key,
      0x56 => Numpad4Key,
      0x57 => Numpad5Key,
      0x58 => Numpad6Key,
      0x59 => Numpad7Key,
      0x5b => Numpad8Key,
      0x5c => Numpad9Key,
      // 0x41 => NumpadDecimalKey,
      // 0x43 => NumpadMultiply,
      // 0x45 => NumpadPlusKey,
      // 0x47 => NumpadClearKey,
      // 0x4B => NumpadDivideKey,
      // 0x4C => NumpadEnterKey,
      // 0x4E => NumpadMinusKey,
      // 0x51 => NumpadEqualsKey,
      0x7a => F1Key,
      0x78 => F2Key,
      0x63 => F3Key,
      0x76 => F4Key,
      0x60 => F5Key,
      0x61 => F6Key,
      0x62 => F7Key,
      0x64 => F8Key,
      0x65 => F9Key,
      0x6d => F10Key,
      0x67 => F11Key,
      0x6f => F12Key,
      0x69 => F13Key,
      0x6b => F14Key,
      0x71 => F15Key,
      0x6a => F16Key,
      0x40 => F17Key,
      0x4f => F18Key,
      0x50 => F19Key,
      0x5a => F20Key,
      0x37 => CommandKey,
      0x38 => LShiftKey,
      0x39 => CapsLockKey,
      // 0x3A => OptionKey,
      0x3b => LControlKey,
      0x3c => RShiftKey,
      // 0x3D => ROption ,
      0x3e => RControlKey,
      // 0x3F => FunctionKey,
      0x73 => HomeKey,
      // 0x1E => RBracket,
      // 0x21 => LBracket ,
      // 0x27 => QuoteKey,
      // 0x29 => Semicolon,
      // 0x2A => Backslash,
      // 0x2B => CommaKey,
      // 0x2C => SlashKey,
      // 0x2F => PeriodKey,
      _ => OtherKey(code),
    }
  }
}

// TODO: find these
impl From<MouseButton> for u32 {
  fn from(button: MouseButton) -> u32 {
    match button {
      LeftButton => 0x01,
      RightButton => 0x02,
      MiddleButton => 0x04,
      X1Button => 0x05,
      X2Button => 0x06,
      OtherButton(code) => code,
    }
  }
}
